package com.test.myjavalib;

import java.util.ArrayList;

public class TestQueue {

    public static void main(String[] args) {

        MyQueue myQueue =  new MyQueue();
        myQueue.push(1);
        myQueue.push(2);
        Integer peek = myQueue.peek();
        System.out.println("peek result : " + peek);
        Integer pop = myQueue.pop();
        System.out.println("pop result : " + pop);
        boolean empty = myQueue.empty();
        System.out.println("empty result : " + empty);
    }
public static class MyQueue {


    public MyStack stack;

    public MyStack stackQueue;


    public MyQueue() {
        stackQueue = new MyStack();
        stack = new MyStack();
    }
    public void push(Integer data) {
        stack.push(data);
    }

    public Integer peek() {
        if (!stackQueue.isEmpty()) {
            return stackQueue.peek();
        }
        if (!stack.isEmpty()) {
            while (!stack.isEmpty()) {
                stackQueue.push(stack.pop());
            }
            return stackQueue.peek();
        }
        return null;
    }

    public Integer pop() {
        if (!stackQueue.isEmpty()) {
            return stackQueue.pop();
        }
        if (!stack.isEmpty()) {
            while (!stack.isEmpty()) {
                stackQueue.push(stack.pop());
            }
            return stackQueue.pop();
        }
        return null;
    }

    public boolean empty() {
        return stackQueue.isEmpty() && stackQueue.isEmpty();
    }
}

public static class MyStack {

    private ArrayList<Integer> datas = new ArrayList<>();
    public void push(Integer data) {
        datas.add(data);
    }
    public Integer pop() {
            if (isEmpty()) {
                return null;
            }
            Integer integer = datas.get(datas.size() - 1);
            datas.remove(datas.size() - 1);
            return integer;
        }

        public Integer peek() {
            if (isEmpty()) {
                return null;
            }
            return datas.get(datas.size() - 1);
        }

        public int size () {
            return datas.size();
        }

        public boolean isEmpty() {
            return size() == 0;
        }
    }
}